package cn.flyboy.test;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

//这个必须使用junit4.9以上才有
@RunWith(SpringJUnit4ClassRunner.class)
//单元测试的时候真实的开启一个web服务，测试完毕就关闭
@WebAppConfiguration
@Transactional
@ContextConfiguration(locations = {"classpath:spring/spring.xml", "classpath:spring/spring-mvc.xml"})
public class testMock {
    /**
     * web应用的上下文
     */
    @Resource
    private WebApplicationContext context;
    /**
     * Mock测试web的核心类
     */
    private MockMvc mockMVC;

    @Before
    public void initMockMVC() {
        this.mockMVC = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Test
    public void testStringResult() throws Exception {

        String res = mockMVC.perform(get("/hello")
                        .param("hello", "This is my")
                        .contentType(MediaType.TEXT_HTML)
                        .accept(MediaType.TEXT_HTML))
                .andExpect(status().isOk())//期望值
                .andDo(print())//打印结果
                .andReturn().getResponse().getContentAsString();//结果字符串
        System.err.println(res);

    }

    @Test
    public void testJsonResult() throws Exception {

        String res = mockMVC.perform(get("/json")
                        .param("json", "JAVA")
                        .param("hah", "hah")
                        //请求参数的类型
                        .contentType(MediaType.TEXT_HTML)
                        //希望服务器返回的值类型
                        .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())//期望值
                .andDo(print())//打印结果
                .andReturn().getResponse().getContentAsString();//结果字符串
        System.err.println(res);
    }

    @Test
    public void testModelResult() throws Exception {

        String res = mockMVC.perform(get("/json")
                        .param("json", "JAVA")
                        .param("hah", "hah")
                        //请求参数的类型
                        .contentType(MediaType.TEXT_HTML)
                        //希望服务器返回的值类型
                        .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())//期望值
                .andDo(print())//打印结果
                .andReturn().getResponse().getContentAsString();//结果字符串
        System.err.println(res);
    }
}
